import { UnauthorizedException } from "@nestjs/common";
var md5 = require('md5');
const JWT = require('jsonwebtoken');

const salt = "cvnert"

export const jwtConstants = {
    secret: salt,
};

//枚举所有的角色
export enum Role {
    ADMIN = 'admin',
    USER = 'user',
    GUEST = 'guest',
}

// 加密
export const encryption = (str) => {
    const newStr = md5(str + salt)
    return newStr
}
/**
 * 验证token
 * @param token token
 * @param cb    回调函数
 */
export const validateToken = (token, cb) => {
    JWT.verify(token, salt, function (err, data) {
        if (err) {
            // console.log(err);
            // cb({ code: false, data: {} });
            throw new UnauthorizedException();
        } else {
            cb({
                code: true,
                data,
            });
        }
    });
};